package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.dsp.bank.directbank.bankInterface.IUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.ReqQryUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfo;
import com.fingard.dsp.bank.reqrespobj.qryunduebillinfo.RetAtsUndueBillInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
/**
 * 获取未到期票据信息
 * @author F
 *
 */
public class CEB02QryUndueBillInfo extends CEB02Base implements IUndueBillInfo{

	@Override
	public RetAtsUndueBillInfo qryUndueBillInfo(ReqQryUndueBillInfo recInfo) throws Exception {
		RetAtsUndueBillInfo retAts = new RetAtsUndueBillInfo(recInfo.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
        String tmpRespInfo = "";
		int PAGE = 0;
		int BISHU = 50;
        try {
        	for(int i=0;i<=PAGE;i++) {
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "G053";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader);  
            //写入body
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//接入机构号
            xmlWriter.writeElementString("DEALER_CODE", recInfo.DealerCode);//接入机构号
            xmlWriter.writeElementString("QISHI", i*BISHU+"");//起始笔数
            xmlWriter.writeElementString("BISHU", BISHU+"");//查询笔数默认50
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    	String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                    	String tmpRetMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                    	if (tmpRetCode.length()==0) {
                    		tmpRespCode = "0";
                            tmpRespInfo = "获取成功";
                            //翻页处理
                            if(PAGE == 0) {
                                int Z_BISHU = Integer.valueOf(tmpBkReader.getFirstText("/Out/Body/Z_BISHU")); 
                                int count = Z_BISHU/BISHU;
                                if(count>0) {
                                	if(Z_BISHU%BISHU==0) {
                                		PAGE = count - 1;
                                	} else {
                                	PAGE = count;
                                	}
                                }
                            }
                            while (tmpBkReader.read("/Out/Body/Frame")) {
                            	RetAtsUndueBillInfoDetail tmpRetDetail = new RetAtsUndueBillInfoDetail();
                                tmpRetDetail.DealerCode = recInfo.DealerCode;
                                tmpRetDetail.BillNo = tmpBkReader.readedXElement.getSingleText("PIAOJU_NO");
                                tmpRetDetail.BillMoney = tmpBkReader.readedXElement.getSingleText("MONEY");
                                tmpRetDetail.BillStartDate = tmpBkReader.readedXElement.getSingleText("KAIPIAO_DATE").replace("-","");
                                tmpRetDetail.BillEndDate = tmpBkReader.readedXElement.getSingleText("DUE_DATE").replace("-","");
                                tmpRetDetail.BailMoney = "";
                                long uUseMoney = Long.parseLong(tmpBkReader.readedXElement.getSingleText("OUTSTAND_MONEY"));
                                tmpRetDetail.RepaymentMoney = (Long.parseLong(tmpRetDetail.BillMoney) - uUseMoney) + "";
                                tmpRetDetail.BailAndRepaymentMoney = (Long.parseLong(tmpRetDetail.BillMoney) + Long.parseLong(tmpRetDetail.RepaymentMoney)) + "";
                                tmpRetDetail.Reserve1 = "";
                                tmpRetDetail.Reserve2 = "";
                                tmpRetDetail.Reserve3 = "";
                                tmpRetDetail.Reserve4 = "";
                                tmpRetDetail.Reserve5 = "";
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                        	tmpRespCode = "-1";
                            tmpRespInfo = tmpRetMsg;
                            break;
                        }                          	
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                    break ;
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
                break ;
            }
         }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
